package logic

import (
	"context"
	"zye6/user_srv/user"

	"zye6/api/internal/svc"
	"zye6/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterByMobileLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRegisterByMobileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterByMobileLogic {
	return &RegisterByMobileLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RegisterByMobileLogic) RegisterByMobile(req *types.RegisByMobileRequest) (resp *types.RegMsgResponse, err error) {
	// todo: 注册

	//查询手机号
	mobile, err := l.svcCtx.UserSrv.FindByMobile(l.ctx, &user.FindByMobileRequest{Mobile: req.Mobile})
	if err != nil {
		return &types.RegMsgResponse{
			Cre:  -1,
			Msg:  "服务器错误",
			Data: nil,
		}, err
	}

	if mobile.UserId == 0 {
		//没有数据的话直接注册
		_, err = l.svcCtx.UserSrv.RegisByMobile(l.ctx, &user.RegisByMobileRequest{
			Mobile: req.Mobile,
			Code:   req.Code,
		})
		if err != nil {
			return &types.RegMsgResponse{
				Cre:  -2,
				Msg:  "服务器错误",
				Data: nil,
			}, err
		}
	}

	return &types.RegMsgResponse{
		Cre:  0,
		Msg:  "登录成功",
		Data: nil,
	}, err
}
